package org.abratuhi.kvngroupware.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.gui.GUIScenarioManager;
import org.abratuhi.kvngroupware.model.Joke;
import org.abratuhi.kvngroupware.model.Scenario;
import org.abratuhi.kvngroupware.network.Message;
import org.jdom.Element;

public class ScenarioManagerController  implements ActionListener, WindowListener, ListSelectionListener{
	

	KVNGroupwareClient kvnclient;
	GUIScenarioManager gui;
	Scenario scenario;
	ArrayList<Joke> jokes;
	
	public ScenarioManagerController(KVNGroupwareClient kvnclient, GUIScenarioManager gui, Scenario scenario, ArrayList<Joke> jokes){
		this.kvnclient = kvnclient;
		this.gui = gui;
		this.scenario = scenario;
		this.jokes = jokes;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if(cmd.equals(GUIScenarioManager.AC_ADD_SCENARIO_JOKE)){
			String[] attrNames = new String[]{"type", "action", "scenarioId"};
			String[] attrValues= new String[]{"application", "add_scenario_joke", scenario.getId()};
			String text = new String();
			Element[] content = (gui.selectedJoke == null)? new Element[0] : new Element[]{gui.selectedJoke.toSmallElement()};
			
			Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
			kvnclient.sendMessage(message);
		}
		else if(cmd.equals(GUIScenarioManager.AC_REMOVE_SCENARIO_JOKE)){
			String[] attrNames = new String[]{"type", "action", "scenarioId"};
			String[] attrValues= new String[]{"application", "remove_scenario_joke", scenario.getId()};
			String text = new String();
			Element[] content = (gui.selectedJoke == null)? new Element[0] : new Element[]{gui.selectedJoke.toSmallElement()};
			
			Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
			kvnclient.sendMessage(message);
		}
		else if(cmd.equals(GUIScenarioManager.AC_UP_SCENARIO_JOKE)){
			String[] attrNames = new String[]{"type", "action", "scenarioId"};
			String[] attrValues= new String[]{"application", "up_scenario_joke", scenario.getId()};
			String text = new String();
			Element[] content = (gui.selectedJoke == null)? new Element[0] : new Element[]{gui.selectedJoke.toSmallElement()};
			
			Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
			kvnclient.sendMessage(message);
		}
		else if(cmd.equals(GUIScenarioManager.AC_DOWN_SCENARIO_JOKE)){
			String[] attrNames = new String[]{"type", "action", "scenarioId"};
			String[] attrValues= new String[]{"application", "down_scenario_joke", scenario.getId()};
			String text = new String();
			Element[] content = (gui.selectedJoke == null)? new Element[0] : new Element[]{gui.selectedJoke.toSmallElement()};
			
			Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
			kvnclient.sendMessage(message);
		}
		else if(cmd.equals(GUIScenarioManager.AC_SAVE_SCENARIO)){
			
		}
	}

	@Override
	public void windowActivated(WindowEvent e) {
	}

	@Override
	public void windowClosed(WindowEvent e) {
	}

	@Override
	public void windowClosing(WindowEvent e) {
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
	}

	@Override
	public void windowIconified(WindowEvent e) {
	}

	@Override
	public void windowOpened(WindowEvent e) {
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		// selection
		if(e.getSource().equals(gui.tableJokes.getSelectionModel())){
			int index = gui.tableJokes.getSelectedRow();
			if(index != -1) gui.selectedJoke = kvnclient.getJoke(index);
		}
		else if(e.getSource().equals(gui.tableScenario.getSelectionModel())){
			int index = gui.tableScenario.getSelectedRow();
			if(index != -1) {
				String jokeId = scenario.get(index);
				if(jokeId != null) gui.selectedJoke = kvnclient.getJoke(jokeId);
			} 
		}
		
		// selected joke's text
		if(gui.selectedJoke != null) gui.textJoke.setText(gui.selectedJoke.getText());
	}

}
